package unlp_fi.Test_LRS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;

import org.json.simple.JSONObject;

public class StatementTest extends AbstractTest {

	public int createStatementTest() {
		try {
			//

			HttpURLConnection conn = super.connectionPOST("statement/create");

			JSONObject actor = new JSONObject();
			actor.put("objectType", "AGENT");
			// actor.put("name", "nombre");
			// actor.put("mbox", "mail");
			actor.put("id", 1);

			JSONObject verb = new JSONObject();
			verb.put("id", 4);

			JSONObject object = new JSONObject();
			object.put("objectType", "ACTIVITY");
			object.put("name", "statement simple");
			object.put("description", "adadaddadaddd");

			JSONObject team = new JSONObject();
			team.put("id", 4);
			team.put("objectType", "GROUP");

			JSONObject context = new JSONObject();
			context.put("team", team);

			JSONObject attachemnt = new JSONObject();
			attachemnt.put("name", "video");
			attachemnt.put("description", "un video en youtube");
			attachemnt
					.put("fileURL",
							"https://www.youtube.com/watch?v=aDrqhJSB78I&list=PL0F54077099487DBD&index=23");

			JSONObject statement = new JSONObject();
			statement.put("objectType", "STATEMENT");
			statement.put("id", 0);
			statement.put("actor", actor);
			statement.put("verb", verb);
			statement.put("object", object);
//			statement.put("context", context);
//			statement.put("attachment", attachemnt);

			StringWriter out = new StringWriter();
			statement.writeJSONString(out);

			String jsonText = out.toString();
			System.out.print(jsonText);

			OutputStream os = conn.getOutputStream();
			os.write(jsonText.getBytes());
			os.flush();

			if (conn.getResponseCode() < 200 & conn.getResponseCode() >= 300) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);

			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
		return 1;

	}

	public void getStatementTest(int l) {

		try {

			HttpURLConnection conn = super.connectionGET("statement/get/" + l);

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
	}

	public void getAllStatementTest(int l) {

		try {

			HttpURLConnection conn = super
					.connectionGET("statement/listStatements/" + l + "/" + 4);

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
	}

	public int addExtensionToStatementTest() {
		try {
			//

			HttpURLConnection conn = super
					.connectionPOST("statement/addExtension");

			JSONObject actor = new JSONObject();
			actor.put("objectType", "AGENT");
			// actor.put("name", "nombre");
			// actor.put("mbox", "mail");
			actor.put("id", 1);

			JSONObject verb = new JSONObject();
			verb.put("id", 4);

			JSONObject object = new JSONObject();
			object.put("objectType", "ACTIVITY");
			object.put("name", "statement simple");
			object.put("description", "adadaddadaddd");

			JSONObject team = new JSONObject();
			team.put("id", 4);
			team.put("objectType", "GROUP");

			JSONObject context = new JSONObject();
			context.put("team", team);

			JSONObject attachemnt = new JSONObject();
			attachemnt.put("name", "video");
			attachemnt.put("description", "un video en youtube");
			attachemnt
					.put("fileURL",
							"https://www.youtube.com/watch?v=aDrqhJSB78I&list=PL0F54077099487DBD&index=23");

			JSONObject statement = new JSONObject();
			statement.put("objectType", "STATEMENT");
			statement.put("id", 0);
			statement.put("actor", actor);
			statement.put("verb", verb);
			statement.put("object", object);
			statement.put("context", context);
			statement.put("attachment", attachemnt);

			StringWriter out = new StringWriter();
			statement.writeJSONString(out);

			String jsonText = "18&" + out.toString();
			System.out.print(jsonText);

			OutputStream os = conn.getOutputStream();
			os.write(jsonText.getBytes());
			os.flush();

			if (conn.getResponseCode() < 200 & conn.getResponseCode() >= 300) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);

			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
		return 1;

	}

}
